#include "advertising.h"

Advertising::Advertising()
{
    process = new QProcess;
    process->setProgram(QCoreApplication::applicationDirPath());
    connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            this, &Advertising::finished);

    /*init config file*/
    QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
    if(!QFile(iniFilePath).exists())
    {
        QMessageBox::critical(nullptr, "ERROR",
                              iniFilePath + " is not exist.");
        qCritical() << iniFilePath + " is not exist.";
    }
    settings = new QSettings(iniFilePath, QSettings::IniFormat);
    settings->setIniCodec("UTF-8");
    mplayerPath = settings->value("ADVERTISING/mplayer_path", "./mplayer").toString();
    playUrl = settings->value("ADVERTISING/play_url").toString();

    qDebug() << "new Advertising succeed";
}

void Advertising::start()
{
    QString cmdString = QString("%1 %2 -zoom -x 800 -y 480").arg(mplayerPath).arg(playUrl);
    process->start(cmdString);
    qDebug() << "starting " + cmdString;
}

void Advertising::kill()
{
    process->kill();
    qDebug() << "advertising killed";
}

void Advertising::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
    qDebug() << "Advertising process finished. exitCode: "
             << exitCode << " ExitStatus: " << exitStatus;
    disconnect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            this, &Advertising::finished);
    w.show();
    w.mplayer->start();
    qDebug() << "MainWindow showed";
}
